<?php

/**
 * This is the model class for table "withdraw".
 *
 * The followings are the available columns in table 'withdraw':
 * @property integer $id
 * @property double $amount
 * @property string $method
 * @property string $request_date
 * @property integer $status
 * @property string $member_id
 * @property string $member_account_id
 * @property integer $member_transaction_id
 * @property integer $company_transaction_id
 *
 * The followings are the available model relations:
 * @property MemberAccount $memberAccount
 * @property Member $member
 */
class Withdraw extends CActiveRecord {

    public $payprocessor;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Withdraw the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'withdraw';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('amount, method, request_date, status, member_id', 'required'),
            array('status, member_transaction_id, company_transaction_id', 'numerical', 'integerOnly' => true),
            array('amount', 'numerical'),
            //array('method', 'length', 'max'=>255),
            array('member_id, member_account_id', 'length', 'max' => 11),
            //array('method', 'exist', 'allowEmpty' => 'true', 'attributeName' => 'payment_processors_id', 'className' => 'MemberAccount', 'criteria' => array(':member_id' => Yii::app()->user->id)),
            array('method', 'exist', 'allowEmpty' => 'true', 'attributeName' => 'payment_processors_id', 'className' => 'MemberAccount', 'criteria' => array('condition' => 'member_id = ' . Yii::app()->user->id), 'message' => 'You\'ve no account for requested {attribute}.'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, amount, method, request_date, status, member_id, member_account_id, member_transaction_id, company_transaction_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'memberAccount' => array(self::BELONGS_TO, 'MemberAccount', 'member_account_id'),
            'member' => array(self::BELONGS_TO, 'Member', 'member_id'),
            'paymentProcessor' => array(self::BELONGS_TO, 'PaymentProcessor', 'method'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'amount' => 'Amount',
            'method' => 'Method',
            'request_date' => 'Request Date',
            'status' => 'Status',
            'member_id' => 'Member',
            'member_account_id' => 'Withdraw Member Account',
            'member_transaction_id' => 'Withdraw Member Transaction',
            'company_transaction_id' => 'Withdraw Company Transaction',            
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $criteria->condition = "status=0";
        $criteria->compare('id', $this->id);
        $criteria->compare('amount', $this->amount);
        $criteria->compare('paymentProcessor.name', $this->payprocessor, true);
        $criteria->compare('request_date', $this->request_date, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('member_id', $this->member_id, true);
        $criteria->compare('member_account_id', $this->member_account_id, true);
        $criteria->compare('member_transaction_id', $this->member_transaction_id);
        $criteria->compare('company_transaction_id', $this->company_transaction_id);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

}